const conn = require('../app/database')
const {
  SERVER_HOST,
	SERVER_PORT,
} = require('../app/config')

class BannerModel {
  async getBannerList() {
    const statement = `
      SELECT bi.id id, bi.type type, 
      CONCAT('${SERVER_HOST}', ":", '${SERVER_PORT}', "/banner/", bi.show_img) showImg, 
      b.name bannerType, 
      JSON_OBJECT('goodsId', g.id, 'spuNo', g.spu_no,  'goodsName', g.goods_name) goodsInfo
      FROM banner_item bi 
      LEFT JOIN banner b ON bi.banner_id = b.id
      LEFT JOIN goods g ON bi.goods_id = g.id;
    `
    const [result] = await conn.execute(statement)
    return result
  }

  async checkBanner(bannerShowImg) {
    const statement = `
      SELECT * FROM banner_item WHERE show_img = ?;
    `
    const [result] = await conn.execute(statement, [bannerShowImg])
    return result.length === 0 ? false : true
  }

}

module.exports = new BannerModel()